﻿#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
const int N = 110;
struct s {
	string name;
	int y;
	int m;
	int d;
	int id;
}arr[N];
bool cmp(s& s1,s& s2)
{
	if (s1.y != s2.y)
	{
		return s1.y < s2.y;
	}
	else if(s1.m!=s2.m)
	{
		return s1.m < s2.m;
	}
	else if(s1.d!=s2.d)
	{
		return s1.d < s2.d;
	}
	else
	{
		return s1.id > s2.id;
	}
}
int main()
{
	int n;
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		cin>>arr[i].name>>arr[i].y>>arr[i].m>>arr[i].d;
		arr[i].id = i;
	}
	sort(arr, arr + n, cmp);
	for (int i = 0; i < n; i++)
	{
		cout << arr[i].name << endl;
	}
	return 0;
}
//const int N = 310;
//struct s
//{
//	int chinese;
//	int math;
//	int english;
//	int total;
//	int id;
//}arr[N];
//bool cmp(s& s1,s& s2)
//{
//	if (s1.total != s2.total)
//	{
//		return s1.total > s2.total;
//	}
//	else if(s1.chinese!=s2.chinese)
//	{
//		return s1.chinese > s2.chinese;
//	}
//	else
//	{
//		return s1.id < s2.id;
//	}
//}
//int main()
//{
//	int n = 0;
//	cin >> n;
//	for (int i = 1; i <= n; i++)
//	{
//		cin >> arr[i].chinese>>arr[i].math>>arr[i].english;
//		arr[i].total = arr[i].chinese + arr[i].math + arr[i].english;
//		arr[i].id = i;
//	}
//	sort(arr+1, arr + n+1,cmp);
//	for (int i = 1; i <= 5; i++)
//	{
//		cout << arr[i].id << " " << arr[i].total << endl;;
//
//	}
//	return 0;
//}
////代码2演示构造和析构函数
//#include <iostream>
//using namespace std;
//struct Stu
//{
//    //成员变量
//    string name;
//    int chinese;
//    int math;
//    int total;
//    //构造函数
//    Stu()
//    {
//        cout << "调用构造函数" << endl;
//        name = "小明";
//        chinese = 99;
//        math = 95;
//        total = math + chinese;
//    }
//    //析构函数
//    ~Stu()
//    {
//        cout << "调用析构函数" << endl;
//        //对于当前这个类的对象没有啥资源清理
//    }
//    void print_stu() //打印
//    {
//        cout << "名字:" << name << endl;
//        cout << "语文: " << chinese << endl;
//        cout << "数学: " << math << endl;
//        cout << "总分: " << total << endl;
//    }
//};
//int main()
//{
//    struct Stu s;
//    //s.print_stu();
//    return 0;
//}
////#include <iostream>
////#include <string>
////#include <algorithm>
////using namespace std;
////const int N = 55;
////int arr[N];
////bool cmp(int a, int b)
////{
////	return a > b;
////}
////int main()
////{
////	int n;
////	cin >> n;
////	for (int i = 0; i < n; i++)
////	{
////		cin >> arr[i];
////	}
////	sort(arr, arr+n,cmp);
////	for (int i = 0; i < 5; i++)
////	{
////		cout << arr[i] << " ";
////	}
////	return 0;
////}
////struct S
////{
////	string name;
////	float tp;
////	int flag;
////
////}s;
////int main()
////{
////	int n;
////	cin >> n;
////	int cnt = 0;
////	while (n--)
////	{
////		cin >> s.name >> s.tp >> s.flag;
////		if (s.tp >= 37.5 && s.flag)
////		{
////			cout << s.name << endl;
////			cnt++;
////		}
////	}
////	cout << cnt << endl;
////	return 0;
////}
////const int N = 110;
////struct S
////{
////	string name;
////	int score;
////}s[N];
//////比较方法
////
////bool cmp(S s1, S s2)
////{
////	return s1.score > s2.score;
////}
////int main()
////{
////	int n = 0;
////	cin >> n;
////	int i = 0;
////	int t = n;
////	//输入数据
////
////	while (t--)
////	{
////		cin >> s[i].score >> s[i].name;
////		i++;
////	}
////	//降序排序
////	sort(s, s + n, cmp);
////	cout<<s[0].name << endl;
////	return 0;
////}
//////#include<iostream>
//////using namespace std;
//////struct s
//////{
//////	int score;
//////	string name;
//////};
//////
//////int main()
//////{
//////	int N;
//////	cin >> N;
//////	struct s max;
//////	cin >> max.score >> max.name;
//////	N--;
//////	s s1;
//////	while (N--)
//////	{
//////		cin >> s1.score >> s1.name;
//////		if (s1.score > max.score)
//////		{
//////			max = s1;
//////		}
//////	}
//////	cout << max.name << endl;
//////	return 0;
//////}
//////struct stu {
//////	int id;
//////	int score1;
//////	int score2;
//////
//////	int calc_total()
//////	{
//////		return score1 + score2;
//////	}
//////	double calc_zonghe() 
//////	{
//////		return score1 * 0.7 + score2 * 0.3;
//////	}
//////};
////////直接判断s是否优秀并打印
//////void judge(stu& s) {
//////	if (s.calc_total() > 140 && s.calc_zonghe() >= 80)
//////	{
//////		cout << "Excellent" << endl;
//////	}
//////	else
//////	{
//////		cout << "Not excellent" << endl;
//////	}
//////}
//////int main()
//////{
//////	stu s;
//////	int N;
//////	cin >> N;
//////	while (N--)
//////	{
//////		cin >> s.id >> s.score1 >> s.score2;
//////		judge(s);
//////	}
//////	return 0;
//////}
//////#include <iostream>
//////using namespace std;
//////
//////class Time {
//////
//////public:
//////    int hours;      // 小时
//////    int minutes;    // 分钟
//////
//////    Time() {
//////        hours = 0;
//////        minutes = 0;
//////    }
//////
//////    Time(int h, int m) {
//////        this->hours = h;
//////        this->minutes = m;
//////    }
//////
//////    void show() {
//////        cout << hours << " " << minutes << endl;
//////    }
//////
//////    // write your code here......
//////    bool operator<(Time& t)
//////    {
//////        if (hours != t.hours)
//////        {
//////            return hours < t.hours;
//////        }
//////        else if (minutes != t.minutes)
//////        {
//////            return minutes < t.minutes;
//////        }
//////        else {
//////            return false;
//////        }
//////
//////    }
//////
//////};
//////
//////int main() {
//////    int h, m;
//////    cin >> h;
//////    cin >> m;
//////
//////    Time t1(h, m);
//////    Time t2(6, 6);
//////
//////    if (t1 < t2)
//////        cout << "yes";
//////    else
//////        cout << "no";
//////    return 0;
//////}
//////////代码2演⽰构造和析构函数
////////#include <iostream>
////////using namespace std;
////////struct Stu
////////{
////////    //成员变量
////////    string name;
////////    int chinese;
////////    int math;
////////    int total;
////////    //构造函数
////////    Stu()
////////    {
////////        cout << "调用构造函数" << endl;
////////        name = "小明";
////////        chinese = 99;
////////        math = 95;
////////        total = math + chinese;
////////    }
////////    //析构函数
////////    ~Stu()
////////    {
////////        cout << "调用析构函数" << endl;
////////        //对于当前这个类的对象没有啥资源清理
////////    }
////////    void print_stu() //打印
////////    {
////////        cout << "名字:" << name << endl;
////////        cout << "语文: " << chinese << endl;
////////        cout << "数学: " << math << endl;
////////        cout << "总分: " << total << endl;
////////    }
////////};
////////int main()
////////{
////////    struct Stu s;
////////    s.print_stu();
////////    return 0;
////////}
//////////#define _CRT_SECURE_NO_WARNINGS 1
//////////#include <iostream>
//////////using namespace std;
//////////
//////////class Time {
//////////
//////////public:
//////////    int hours;      // 小时
//////////    int minutes;    // 分钟
//////////
//////////    Time() {
//////////        hours = 0;
//////////        minutes = 0;
//////////    }
//////////
//////////    Time(int h, int m) {
//////////        this->hours = h;
//////////        this->minutes = m;
//////////    }
//////////
//////////    void show() {
//////////        cout << hours << " " << minutes << endl;
//////////    }
//////////
//////////    // write your code here......
//////////    Time operator+(Time& t) {
//////////        Time ret;
//////////        int flag = 0;
//////////        ret.minutes = minutes + t.minutes;
//////////        if (ret.minutes >= 60)
//////////        {
//////////            ret.minutes -= 60;
//////////            flag = 1;
//////////        }
//////////        ret.hours = hours + t.hours + flag;
//////////        return ret;
//////////    }
//////////
//////////};
//////////
//////////int main() {
//////////
//////////    int h, m;
//////////    cin >> h;
//////////    cin >> m;
//////////
//////////    Time t1(h, m);
//////////    Time t2(2, 20);
//////////
//////////    Time t3 = t1 + t2;
//////////    t3.show();
//////////
//////////    return 0;
//////////}